const time = {}
//数据转化  
function formatNumber(n) {
	n = n.toString()
	return n[1] ? n : '0' + n
}

// 时间戳转 2020-10-01 00:00:00  Y/M/D h:m:s
time.timeNumToTxt = function(number, format = 'h:m') {
	
	var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
	var returnArr = [];

	var date = new Date(number * 1000);
	returnArr.push(date.getFullYear());
	returnArr.push(formatNumber(date.getMonth() + 1));
	returnArr.push(formatNumber(date.getDate()));

	returnArr.push(formatNumber(date.getHours()));
	returnArr.push(formatNumber(date.getMinutes()));
	returnArr.push(formatNumber(date.getSeconds()));
	for (var i in returnArr) {
		format = format.replace(formateArr[i], returnArr[i]);
	}
	return format;
}
// 获取时间戳 2020-10-01 00:00:00  这种格式的转时间戳
time.txtToTimeNum = function(time) {
	return Date.parse(new Date((time.replace(/-/g, '/')) || '')) / 1000;
}

// 时间转  2020-08-11T17:00:00  转  2020-08-11   17:00
time.tTimeTo = (time) => {
	let arr = time.split('T')
	let arr2 = arr[1].split(':')
	return (arr[0] + ' ' + arr2[0] + ':' + arr2[1]).replace(/-/g, '/')
},
//  时间转  2020-08-11T17:00:00  转  Y/M/D h:m:s
time.hasTToTimeNum = function(Vtime, format = 'h:m') {

	let timeNum = Date.parse(new Date(time.tTimeTo(Vtime) || '')) / 1000;
	return time.timeNumToTxt(timeNum, format)
}
// js 根据秒数获取多少小时，多少分钟，多少秒
time.showTime = function(val,isChina = true) {

	let second,minute,hour,day = '';

	second = `${val % 60 }${isChina?'秒':'s'}`;

	if(val > 60 ){
		minute = Math.floor(val / 60); // 分钟
	}

	if(minute > 60){
		hour = Math.floor(minute / 60); // 小时数

		console.log("hour");
		console.log(hour);
		minute = Math.floor(minute % 60); // 余分钟
	}
	minute = minute? `${minute}${isChina ? '分钟' : 'min'}` : ''
	if(hour > 24){
		day = `${Math.floor(hour / 24)}${isChina ? '天' : 'day'}`; // 天数
		hour = Math.floor(hour % 24); // 余小时
	}
	hour = hour ? `${hour}${hour ? '小时' : 'h'}` : ''

	return `${day}${hour}${minute}${second}`

	// if (val < 60) {
	//
	// 	return `${val}${isChina ? '秒' : 's'}`
	// 	// return val + isChina ? "秒" : 's';
	// 	// return val +  's';
	// } else {
	// 	var min_total = Math.floor(val / 60); // 分钟
	// 	var sec = Math.floor(val % 60); // 余秒
	// 	if (min_total < 60) {
	//
	// 		return `${min_total}${isChina ? '分钟' : 'min'}`
	// 		// return min_total  + isChina ? "分钟" : 'min';
	// 		// return min_total + "分钟" + sec + "秒";
	// 	} else {
	// 		var hour_total = Math.floor(min_total / 60); // 小时数
	// 		var min = Math.floor(min_total % 60); // 余分钟
	//
	//
	// 		return `${hour_total}${isChina ? '小时' : 'h'}${min}${isChina ? '分钟' : 'min'}`
	// 		// return (hour_total +  isChina ? "小时" : 'h') + (min +  isChina ? "分钟" : 'min');
	// 	}
	// }
}

// 是否为今天  val 为2018/08/08格式
time.isToday = function(val){
	return new Date(val).toDateString() === new Date().toDateString()
}
// 获取昨天日期 
time.yesterday = function(){
	var day1 = new Date();
	day1.setTime(day1.getTime()-24*60*60*1000);
	return day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate()
}


// js 获取最近 一周 一个月的日期数组
time.getDayList = function(dayNum) {
	let data1 = []

	function getDay(day) {
		var today = new Date();
		var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
		today.setTime(targetday_milliseconds);
		var tYear = today.getFullYear();
		var tMonth = today.getMonth();
		var tDate = today.getDate();
		tMonth = doHandleMonth(tMonth + 1);
		tDate = doHandleMonth(tDate);
		return tYear + "/" + tMonth + "/" + tDate; //年月日
		// return  tMonth + "/" + tDate; //月日
	}

	function doHandleMonth(month) {
		var m = month;
		if (month.toString().length == 1) {
			m = "0" + month;
		}
		return m;
	}

	function dsdd(e) { //拼接
		data1.splice(0); //请控之前的数据
		for (let i = 0; i < e; i++) {
			data1.unshift(getDay(-i)); //-i 代表之前  i代表将来
		}
	}
	dsdd(dayNum)
	return data1
}
// 时间跳转到时间选择的页面
time.jumpToTimePage = function(option = {
	type: 'rangetime'
}) {
	uni.navigateTo({
		url: "/pages/plusPage/timePage/timePage?timeInfo=" + JSON.stringify(option),
		animationType: "slide-in-bottom",
		success: () => {
			uni.$off('listenChooseTime')
			uni.$on('listenChooseTime', (res) => {
				console.log(res,this)
				uni.navigateBack({
					success: () => {
						uni.$emit('noRefresh')
						option.callback(res)
					}
				})
			})
		}
	})

}
// JS获取两个日期间的所有日期
//获取两日期之间日期列表函数  '2018/07/25'; //开始日期   '2018/08/02'; //结束日期
time.getdiffdate = function(stime, etime) {
	//初始化日期列表，数组
	var diffdate = new Array();
	var i = 0;
	//开始日期小于等于结束日期,并循环
	while (new Date(stime).getTime() <= new Date(etime).getTime()) {
		diffdate[i] = stime;

		//获取开始日期时间戳
		var stime_ts = new Date(stime).getTime();
		// console.log('当前日期：'+stime   +'当前时间戳：'+stime_ts);

		//增加一天时间戳后的日期
		var next_date = stime_ts + (24 * 60 * 60 * 1000);

		//拼接年月日，这里的月份会返回（0-11），所以要+1
		var next_dates_y = new Date(next_date).getFullYear() + '/';
		var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) + '/' : (
			new Date(next_date).getMonth() + 1) + '/';
		var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date(next_date).getDate();

		stime = next_dates_y + next_dates_m + next_dates_d;

		//增加数组key
		i++;
	}
	// console.log(diffdate);
	return diffdate
}
// 获取两个日期之间的月份
time.getMonths = function (date_one, date_tow) {
	let one = date_one.split('/')
	let tow = date_tow.split('/')
  var field =[]
  var fieldValue =[]
	const y2 = parseInt(tow[0])
	let y1 = parseInt(one[0])
	const m2 = parseInt(tow[1])
	let m1 = parseInt(one[1])

	let count = (y2 - y1) * 12 + m2 - m1
	for (let i = 0; count >= 0;) {
		if (m1 <= 12) {
			m1 = m1 < 10 ? ('0' + m1) : m1
			let temp = y1 + '/' + m1
			// this.field = [ ]
			// 结果格式 2020-01
			field.push(temp)
			//  fieldValue = [ ]
			// 结果格式 2020-01-28
			fieldValue.push(temp + '/' + '28')
				++m1
			count--
			++i
		} else {
			m1 -= 12
			y1++
		}
	}
	return {
		field,fieldValue
	}
}
export default time
